import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import store from "@/store";
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => {});
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  // {
  // path: '/about',
  // name: 'about',
  // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
  {
    path: "/free",
    name: "free",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Free.vue"),
  },
  {
    path: "/goods",
    name: "goods",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Goods.vue"),
  },
  {
    path: "/order",
    name: "order",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Order.vue"),
  },
  {
    path: "/user",
    name: "user",
    component: () => import("../views/User.vue"),
    redirect: "/user/cart",
    children: [
      {
        path: "cart",
        component: () => import("../components/user/Cart.vue"),
      },
    ],
    // 路由独享守卫  参数同全局守卫一致
    /* beforeEnter: (to, from, next) => {
      let token = localStorage.getItem("x-auth-token");
      // 如果存在正常进入user页面
      if (token) {
        next();
        // 否则提示你还没有登录
      } else {
        // alert("你还没有登录");
        store.dispatch("showToast/asyncIsShowToast", {
          msg: "你还没有登录",
          type: "warning",
        });
      }
    },*/
  },
  {
    path: "/details",
    name: "details",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Details.vue"),
  },
  // 匹配所有地址(当前路由都没有匹配上时,显示Error-404组件)
  {
    path: "*",
    name: "Error",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Error.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 导航守卫
/*router.beforeEach((to, from, next) => {
  //  如果你准备去user页面,则需要获取是否存在token,
  if (to.path == "/user") {
    let token = localStorage.getItem("x-auth-token");
    // 如果存在正常进入user页面
    if (token) {
      next();
      // 否则提示你还没有登录
    } else {
      // alert("你还没有登录");
      store.dispatch("showToast/asyncIsShowToast", {
        msg: "你还没有登录",
        type: "warning",
      });
    }
    return;
  }
  next();
});*/
export default router;
